﻿using System;
using System.Windows;
using Microsoft.Win32;
using System.IO;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;
using System.Text;

namespace Base64CSharp
{
    class MainMenu : Menu
    {
        private MainWindow parentWindow;
        public MainMenu(MainWindow parentWindow)
        {
            this.parentWindow = parentWindow;

            MenuItem itemFile = new MenuItem();
            itemFile.Header = "_File";
            
            MenuItem itemEncode = new MenuItem();
            itemEncode.Header = "_Encode text";
            itemEncode.Click += EncodeTextOnClick;
            itemFile.Items.Add(itemEncode);

            MenuItem itemEncodeFile = new MenuItem();
            itemEncodeFile.Header = "Encode f_ile...";
            itemEncodeFile.Click += EncodeFileOnClick;
            itemFile.Items.Add(itemEncodeFile);

            MenuItem itemDecodeText = new MenuItem();
            itemDecodeText.Header = "_Decode text";
            itemDecodeText.Click += DecodeTextOnClick;
            itemFile.Items.Add(itemDecodeText);

            MenuItem itemDecodeFile = new MenuItem();
            itemDecodeFile.Header = "Dec_ode file...";
            itemDecodeFile.Click += DecodeFileOnClick;
            itemFile.Items.Add(itemDecodeFile);

            MenuItem itemSaveToFile = new MenuItem();
            itemSaveToFile.Header = "Save to file...";
            itemSaveToFile.Click += SaveToFileOnClick;
            itemFile.Items.Add(itemSaveToFile);

            itemFile.Items.Add(new Separator());

            MenuItem itemExit = new MenuItem();
            itemExit.Header = "E_xit";
            itemExit.Click += ExitOnClick;
            itemFile.Items.Add(itemExit);
            Items.Add(itemFile);
            
            MenuItem itemHelp = new MenuItem();
            itemHelp.Header = "_Help";

            MenuItem itemAbout = new MenuItem();
            itemAbout.Header = "_About";
            itemAbout.Click += AboutOnClick;
            itemHelp.Items.Add(itemAbout);
            Items.Add(itemHelp);
        }
        private void EncodeTextOnClick(object sender, RoutedEventArgs args)
        {
            //System.Text.UnicodeEncoding.UTF8.GetBytes(txtDecoded.Text);
            byte[] source = UnicodeEncoding.UTF8.GetBytes(parentWindow.GetTextContent());
            parentWindow.SetTextContent(Convert.ToBase64String(source));    
        }
        private void EncodeFileOnClick(object sender, RoutedEventArgs args)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if ((bool) dlg.ShowDialog()) {
                FileInfo file = new FileInfo(dlg.FileName);
                FileStream fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read);
                byte[] buffer = new byte[file.Length];
                fs.Read(buffer, 0, Convert.ToInt32(file.Length));
                parentWindow.SetTextContent(Convert.ToBase64String(buffer));
            }
        }
        private void DecodeTextOnClick(object sender, RoutedEventArgs args)
        {
            string source = parentWindow.GetTextContent();
            byte[] result = Convert.FromBase64String(source);
            SaveFileDialog sdlg = new SaveFileDialog();
            if ((bool)sdlg.ShowDialog())
            {
                FileStream sfs = new FileStream(sdlg.FileName, FileMode.Create, FileAccess.Write);
                sfs.Write(result, 0, Convert.ToInt32(result.Length));
                sfs.Close();
            };
        }
        private void DecodeFileOnClick(object sender, RoutedEventArgs args)
        {
            OpenFileDialog odlg = new OpenFileDialog();
            if ((bool) odlg.ShowDialog()) {
                string source = File.ReadAllText(odlg.FileName);
                byte[] result = Convert.FromBase64String(source);
                SaveFileDialog sdlg = new SaveFileDialog();
                if ((bool)sdlg.ShowDialog())
                {
                    FileStream sfs = new FileStream(sdlg.FileName, FileMode.Create, FileAccess.Write);
                    sfs.Write(result, 0, Convert.ToInt32(result.Length));
                    sfs.Close();
                };
            };
        }
        private void SaveToFileOnClick(object sender, RoutedEventArgs args)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.FileName = "Document";
            dlg.DefaultExt = ".txt";
            dlg.Filter = "Text documents (.txt)|*.txt";
            if ((bool) dlg.ShowDialog())
            {
                FileInfo file = new FileInfo(dlg.FileName);
                StreamWriter sw = file.CreateText();
                sw.Write(parentWindow.GetTextContent());
                sw.Close();
            };
        }
        private void ExitOnClick(object sender, RoutedEventArgs args)
        {
            Application.Current.MainWindow.Close();
        }
        private void AboutOnClick(object sender, RoutedEventArgs args)
        {
            MessageBox.Show(parentWindow, "Base64CSharp version " + parentWindow.GetVersion(), "About", MessageBoxButton.OK);
        }
    }
}
